package com.itany.nmms.service.impl;

import com.itany.nmms.constant.StatusConstant;
import com.itany.nmms.dao.ProductTypeMapper;
import com.itany.nmms.entity.ProductType;
import com.itany.nmms.entity.ProductTypeExample;
import com.itany.nmms.exception.ProductTypeExistException;
import com.itany.nmms.exception.RequestParamErrorException;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/8/2 11:22
 * Version:1.0-SNAPSHOT
 * Description:
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper typeMapper;

    @Override
    public void addType(String name) throws RequestParamErrorException, ProductTypeExistException {
        if(ParameterUtil.isNull(name)){
            throw new RequestParamErrorException("请求参数有误");
        }
        // 商品类型名称不能重复
        ProductTypeExample example = new ProductTypeExample();
        example.or()
                .andNameEqualTo(name);
        List<ProductType> types = typeMapper.selectByExample(example);
        if(!types.isEmpty()){
            throw new ProductTypeExistException("该商品类型已经存在");
        }

        // 创建一个商品类型对象,用于保存
        ProductType type = new ProductType();
        type.setName(name);
        // 为该对象设置其状态的默认值
        type.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        // 添加商品类型
        typeMapper.insertSelective(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<ProductType> findAll() {
        List<ProductType> types = typeMapper.selectByExample(new ProductTypeExample());
        return types;
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ProductType findById(String id) throws RequestParamErrorException {
        if(ParameterUtil.isNull(id)){
            throw new RequestParamErrorException("请求参数有误");
        }
        ProductType type = typeMapper.selectByPrimaryKey(Integer.parseInt(id));
        return type;
    }

    @Override
    public void modifyName(String id, String name) throws RequestParamErrorException, ProductTypeExistException {
        if(ParameterUtil.isNull(id) || ParameterUtil.isNull(name)){
            throw new RequestParamErrorException("请求参数有误");
        }
        // 类型名称不能重复,但可以是原来的
        ProductTypeExample example = new ProductTypeExample();
        example.or()
                .andIdNotEqualTo(Integer.parseInt(id))
                .andNameEqualTo(name);
        List<ProductType> types = typeMapper.selectByExample(example);
        if(!types.isEmpty()){
            throw new ProductTypeExistException("该商品类型已经存在");
        }
        // 修改类型名称
        ProductType type = new ProductType();
        type.setId(Integer.parseInt(id));
        type.setName(name);
        typeMapper.updateByPrimaryKeySelective(type);
    }

    @Override
    public void modifyStatus(String id) throws RequestParamErrorException {
        if(ParameterUtil.isNull(id)){
            throw new RequestParamErrorException("请求参数有误");
        }
        // 根据主键查询当前商品类型信息
        ProductType type = typeMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED.equals(type.getStatus())){
            // 如果当前商品类型的状态为启用
            // 则将其状态改为禁用
            type.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_DISABLED);
        }else{
            // 否则,将其状态改为启用
            type.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        }
        typeMapper.updateByPrimaryKeySelective(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<ProductType> findEnable() {
        ProductTypeExample example = new ProductTypeExample();
        example.or()
                .andStatusEqualTo(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        List<ProductType> types = typeMapper.selectByExample(example);
        return types;
    }
}
